home *** CD-ROM | disk | FTP | other *** search
- //
- // MiscUniqueStringArray.h -- never stores duplicates of the same string...
- // Written by Bill Bumgarner
- // Copyright (c) 1994 by Bill Bumgarner.
- // Version 1.0. All rights reserved.
- // This notice may not be removed from this source code.
- //
- // This object is included in the MiscKit by permission from the author
- // and its use is governed by the MiscKit license, found in the file
- // "LICENSE.rtf" in the MiscKit distribution. Please refer to that file
- // for a list of all applicable permissions and restrictions.
- //
-
- #import <misckit/misckit.h>
-
- @implementation MiscUniqueStringArray:MiscStringArray
- - (void) _allocateUniqueStringHashTable
- {
- uniqueStringHash = NXCreateHashTableFromZone(NXStrPrototype,
- 0,
- NULL,
- [self zone]);
- }
-
- - init
- {
- if (![super init]) return nil;
-
- [self _allocateUniqueStringHashTable];
-
- return self;
- }
-
- - free
- {
- NXFreeHashTable(uniqueStringHash);
- return [super free];
- }
-
- - addString:(const char *)aString
- {
- if(!aString)
- return (nil);
-
- /* NXHashInsert() returns NULL iff. aString is not already in
- the table -- if NXHashInsert() returns NULL, then tell super
- to add the string to the array. If not, just return self
- [if the caller needs to know whether or not the string was
- already in the table, it is the caller's responsibility to
- check!]
- */
- if( NXHashInsert(uniqueStringHash, aString) == NULL )
- return [super addString:aString];
- else
- return self;
- }
-
- - removeString:(const char *)aString
- {
- if(!aString)
- return (nil);
-
- if( [super removeString:aString] == nil )
- return (nil);
-
- NXHashRemove(uniqueStringHash, aString);
-
- return self;
- }
-
- - insertString:(const char *)aString at:(unsigned int)index
- {
- if(!aString)
- return (nil);
-
- /* same thinking as addString:...
- */
- if( NXHashInsert(uniqueStringHash, aString) == NULL )
- return [super insertString:aString at:index];
- else
- return self;
- }
-
-
- - (BOOL) isMember:(const char *)aString
- {
- return (aString && (NXHashMember(uniqueStringHash, aString) != 0) );
- }
-
- - awake
- {
- id *i, *max;
-
- [self _allocateUniqueStringHashTable];
-
- /* since we KNOW that it was an instance of MiscUniqueStringArray
- that was written, we KNOW that all unarchived strings will
- be unique to this array. Therefore, rebuild the unique string
- hash be traversing the array and creating hash entries for
- each string.
- */
-
- for (i = NX_ADDRESS(strings), max = i + [strings count]; i<max; i++)
- NXHashInsert(uniqueStringHash, [*i stringValue]);
-
- return self;
- }
- @end
-